﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using System;

namespace Micro.Extensions.Log.Kafka
{
    public static class KafkaLogWriterExtension
    {
        public static ILoggingBuilder AddKafkaWriter(this ILoggingBuilder builder, IConfiguration configuration)
        {
            builder.AddConfiguration();
            builder.Services.Configure<KafkaClientConfig>(configuration.GetSection("MicroLog:Kafka"));

            builder.Services.RemoveAll<ILoggerWriter>();
            builder.Services.AddSingleton<ILoggerWriter, KafkaLoggerWriter>();
            builder.Services.AddSingleton<IKafkaClient, KafkaClient>();
            return builder;
        }

        public static ILoggingBuilder AddKafkaWriter(this ILoggingBuilder builder, Action<KafkaClientConfig> configure)
        {
            builder.Services.Configure(configure);

            builder.Services.RemoveAll<ILoggerWriter>();
            builder.Services.AddSingleton<ILoggerWriter, KafkaLoggerWriter>();
            builder.Services.AddSingleton<IKafkaClient, KafkaClient>();
            return builder;
        }
    }
}
